package com.tongzhan.controller;

import com.tongzhan.base.BaseController;
import com.tongzhan.base.Return;
import com.tongzhan.entity.party.ActionCenter;
import com.tongzhan.entity.party.ActionSite;
import com.tongzhan.param.ActionParam;
import com.tongzhan.param.ActionSiteParam;
import com.tongzhan.utils.SnowFlakeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;

/**
 * 申请活动场地
 *
 * @author htmic
 * @date 2020/7/29
 */
@RequestMapping("/action")
@RestController
public class ActionSiteController extends BaseController {

    /**
     * 添加
     *
     * @param param
     * @return
     */
    @PostMapping("/addSite")
    public Return addSite(@RequestBody ActionSiteParam param) {
        if (param.getAction_content() == null || param.getAction_content().trim().length() > 200) {
            return Return.getErrorResult("内容过长");
        }
        if (param.getPredictTime() == null) {
            return Return.getErrorResult("预计活动时间不能为空");
        }
        if (param.getAction_address() == null) {
            return Return.getErrorResult("场地不能为空");
        }
        if (param.getActionTypeId() == null) {
            return Return.getErrorResult("活动类型不能为空");
        }
        if (param.getAction_address()==null) {
            return Return.getErrorResult("申请场地不能为空");
        }

        ActionSite actionSite = new ActionSite();
        BeanUtils.copyProperties(param, actionSite);
        actionSite.setId(new SnowFlakeUtils(3, 3).nextId() + "");
        actionSite.setAddUserId(param.getUserId());

        long l = actionSiteService.addActionSite(actionSite);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }


    /**
     * 列表
     *
     * @param page
     * @param size
     * @param userId         用户id
     * @param year_and_month YYYY-MM 如果是全年：YYYY
     * @return
     */
    @PostMapping("/siteList")
    public Return actionList(Integer page, Integer size, String userId, String year_and_month) {
        page = page == null ? 0 : page - 1;
        List list = actionSiteService.actionSiteList(page, size, userId, year_and_month);
        return Return.getSuccessResult(list);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @PostMapping("/siteDetail")
    public Return getDetail(String id,String userId) {
        if (id == null) {
            return Return.getErrorResult("缺少id参数!");
        }
        ActionSite actionSite = actionSiteService.getDetail(id,userId);
        return Return.getSuccessResult(actionSite);
    }


    /**
     * 审核申请场地
     *
     * @param id
     * @param type   0是不同意 1是同意
     * @param userId 审核人id
     * @return
     */
    @PostMapping("/auditSite")
    public Return auditSite(String id, Integer type,String auditContent, String userId) {
        if (StringUtils.isEmpty(id) || type == null) {
            return Return.getErrorResult("参数为空");
        }
        long flag = actionSiteService.auditSite(id, type,auditContent, userId);
        if (flag > 0) {
            return Return.getSuccessResult("审核成功!");
        }
        return Return.getErrorResult("审核失败!");
    }
}
